package top.kaoshanji.p2ch07a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * 代码清单 p2ch07a-01：产生内存泄露的先进先出队列的实现
 * author kaoshanji
 * ^_^
 * create time 2025/1/19 18:57
 */
public class LeakingQueue<T> {

    private static final Logger logger = LogManager.getLogger(LeakingQueue.class);

    private List<T> backendList = new ArrayList<>();
    private int topIndex = 0;
    public void enqueue(T value) {
        backendList.add(value);
    }
    public T dequeue() {
        T result = backendList.get(topIndex);
        topIndex++;
        return result;
    }

    public static void main(String[] args) {
        logger.info("...代码清单 p2ch07a-01：产生内存泄露的先进先出队列的实现...");

        LeakingQueue<String> stack = new LeakingQueue<>();

        stack.enqueue("Hello");
        stack.enqueue("World");
        stack.dequeue();
        stack.enqueue("Goodbye");
        String value = stack.dequeue();
        logger.info("...value:{}...", value);

        logger.info("...topIndex:{}...", stack.topIndex);

    }


}
